import random

from scipy.spatial import distance

import HFSP_test.PMX_utils as pmx
import numpy as np

from TWVRP_new.twvrp_model import TWVRPModel
from TWVRP_test.twvrp_utils import TWVRPAssignment


class TWVRPModelGA:
    def __init__(self, job_list, time_interval=8, crossover_prob=0.8, mutation_prob=0.1, pop_size=10, chrom_size=8,
                 select_ratio=0.01,
                 generation=500):
        self.job_list = job_list
        self.time_interval = time_interval
        self.crossover_prob = crossover_prob
        self.mutation_prob = mutation_prob
        self.pop_size = pop_size
        self.chrom_size = chrom_size
        self.select_ratio = select_ratio
        self.generation = generation
        self.population = []

    # 工作安排的初始化
    def init_population(self):
        fix_sequence_init = []
        slide_sequence_init = []
        # 每八个小时就是一个数组
        # 先求最大值
        max_start_time = 0
        for job in self.job_list:
            if job['job_class'] == 2:
                start_time = job['start_time']
                if start_time >= max_start_time:
                    max_start_time = start_time
                else:
                    pass
            else:
                pass
        # 计算每个时间窗里的任务
        for i in range(0, max_start_time, 8):
            fix_temp = []
            for job in job_list:
                if job['job_class'] == 2:
                    start_time = job['start_time']
                    if start_time >= i and start_time < i + 8:
                        fix_temp.append(job['job_id'])
                    else:
                        pass
                else:
                    pass
            if len(fix_temp)!=0:
                fix_sequence_init.append(fix_temp)
            else:
                pass
        print (fix_sequence_init)


def create_data():
    # 任务信息,wokr_cost时间开销，单位小时；job_class任务类别，
    # 1为优先任务，2为时间窗任务
    job_list = [
        {'job_id': 0, 'job_name': '1号任务',
         'job_location': 'A地', 'lon': 6, "lat": 6,
         'work_cost': 2.5, 'job_class': 2, 'start_time': 8,
         'end_time': 16},
        {'job_id': 1, 'job_name': '2号任务',
         'job_location': 'B地', 'lon': 8, "lat": 7,
         'work_cost': 2.5, 'job_class': 2, 'start_time': 8,
         'end_time': 16},
        {'job_id': 2, 'job_name': '3号任务',
         'job_location': 'C地', 'lon': 2, "lat": 2,
         'work_cost': 2.5, 'job_class': 2, 'start_time': 24,
         'end_time': 32},
        {'job_id': 3, 'job_name': '4号任务',
         'job_location': 'D地', 'lon': 1, "lat": 4,
         'work_cost': 2.5, 'job_class': 2, 'start_time': 32,
         'end_time': 40},
        {'job_id': 4, 'job_name': '5号任务',
         'job_location': 'E地', 'lon': 9, "lat": 3,
         'work_cost': 2.5, 'job_class': 2, 'start_time': 32,
         'end_time': 40},
        {'job_id': 5, 'job_name': '6号任务',
         'job_location': 'F地', 'lon': 7, "lat": 2,
         'work_cost': 1, 'job_class': 1, 'start_time': None,
         'end_time': None},
        {'job_id': 6, 'job_name': '7号任务',
         'job_location': 'G地', 'lon': 5, "lat": 3,
         'work_cost': 1.5, 'job_class': 1, 'start_time': None,
         'end_time': None},
    ]
    company_info = {
        'company_name': '公司', 'lon': 4, "lat": 4
    }
    # 固定生成8*8的交通开销矩阵，默认第一行第二列为为第一个地点到第二个地点的时间开销，
    # 最后一行为公司到各个地点的开销，这样保证序号都是对的上，每行最后一列表示该点到公司的开销
    transport_cost_matrix = []
    points_list = []
    for index, value in enumerate(job_list):
        points_list.append((value['lon'], value['lat']))
    # 最后还要加上公司
    points_list.append((company_info['lon'], company_info['lat']))
    transport_cost_matrix = distance.cdist(points_list, points_list, 'euclidean')
    # 由于数字太大，全部除以2,且保留一位小数，比如2.2表示需要2.2小时即2小时12分
    transport_cost_matrix = np.around(transport_cost_matrix / 3, decimals=1)
    return job_list, transport_cost_matrix, company_info


if __name__ == '__main__':
    job_list, transport_cost_matrix, company_info = create_data()
    mga = TWVRPModelGA(job_list, time_interval=8, crossover_prob=0.8, mutation_prob=0.1, pop_size=500, chrom_size=9,
                       select_ratio=0.1,
                       generation=200)
    mga.init_population()

    pass
